汉诺塔移动轨迹程序c++
思想
- 递归
- 辅助空间
代码区
#include<iostream>
using namespace std;
bool Hano(int n, char A, char B, char C)
{
if (n == 1)
{
cout << "移动圆圈数" << n << "从盘" << A << "盘" << B << endl;
return true;
}
else
{
Hano(n - 1, A, C, B);
cout << "移动圆圈数" << n << "从盘" << A << "盘" << C << endl;
Hano(n - 1, C, B, A);
}
}
int main()
{
int n;
while (cin >> n)
{
bool Judge = Hano(n, 'l', 'r', 'm');
if (Judge)
cout << "移动完成" << endl;
}
return 0;
}